package io.github.talelin.latticy.controller.v1;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.bo.BannerWithItemsBO;
import io.github.talelin.latticy.bo.CategoryWithChildBO;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.dto.BannerDTO;
import io.github.talelin.latticy.dto.BannerItemDTO;
import io.github.talelin.latticy.mapper.BannerMapper;
import io.github.talelin.latticy.model.BannerDO;
import io.github.talelin.latticy.model.BannerItemDO;
import io.github.talelin.latticy.service.BannerItemService;
import io.github.talelin.latticy.service.BannerService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;
import java.util.List;

@RequestMapping("/v1/banner")
@RestController
@Validated
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @Autowired
    private BannerItemService bannerItemService;

//    @LoginRequired 只要登录就能访问
//    @AdminRequired 只要管理员就能访问
//    @GroupRequired 使接口被保护，默认不能访问，指定分组才能访问
//    @PermissionModule(value = "权限模块")
//    @PermissionMeta(vaule="权限名字")


    /** -------------- banner -------------- */

    // 新增
    @PostMapping()
    public CreatedVO create(@RequestBody @Validated BannerDTO dto) {
        bannerService.create(dto);
        return new CreatedVO();
    }

    // 删除单个
    @DeleteMapping("/{id}")
    public DeletedVO delete(@PathVariable @Positive Integer id) {
        bannerService.delete(id);
        return new DeletedVO();
        // 级联删除 逻辑删除
    }

    // 更新单个
    @PutMapping("/{id}")
    public UpdatedVO update(
            @RequestBody @Validated BannerDTO dto,
            @PathVariable @Positive Integer id
    ) {
        bannerService.update(dto,id);
        return new UpdatedVO(2);
    }

    // 查询单个关联
    @GetMapping("/{id}")
    public BannerWithItemsBO getWithItems(@PathVariable @Positive Integer id){
        return bannerService.getWithItems(id);
    }

    // 不做分页查全部
    @GetMapping("/all")
    public List<BannerDO> getBanners() {
        return bannerService.getBanners();
    }

    // 分页查询
    @GetMapping("/page")
    public PageResponseVO<BannerDO> getBanners(@RequestParam(required = false,defaultValue = "0")
                                               @Min(value = 0) Integer page,
                                               @RequestParam(required = false,defaultValue = "10")
                                               @Min(value = 1) @Max(value = 30) Integer count) {

        Page<BannerDO> pager =new Page<>(page,count);
        IPage<BannerDO> paging = bannerService.getBaseMapper().selectPage(pager,null);

        return new PageResponseVO<>(paging.getTotal(),paging.getRecords(),paging.getCurrent(),paging.getSize());
    }

    /** -------------- bannerItem -------------- */

    @PostMapping("item")
    public CreatedVO creatItem(@RequestBody @Validated BannerItemDTO dto) {
        BannerItemDO bannerItemDO = new BannerItemDO();
        BeanUtils.copyProperties(dto,bannerItemDO);
        bannerItemService.save(bannerItemDO);
        return new CreatedVO();
    }

    @DeleteMapping("item/{id}")
    public DeletedVO deleteItem(@PathVariable @Positive Integer id) {
        bannerItemService.delete(id);
        return new DeletedVO();
    }

    @PutMapping("item/{id}")
    public UpdatedVO updateItem(
            @RequestBody @Validated BannerItemDTO dto,
            @PathVariable @Positive Integer id
    ){
        bannerItemService.update(dto,id);
        return new UpdatedVO();
    }

    @GetMapping("item/{id}")
    public BannerItemDO getItem(@PathVariable @Positive Integer id) {
        BannerItemDO bannerItem = bannerItemService.getById(id);
        if(bannerItem == null) {
            throw new NotFoundException(20001);
        }
        return bannerItem;
    }

}

